home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / Jotto ][ 1.2 / source / Shell ƒ / menus.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  10.2 KB  |  393 lines  |  [TEXT/MMCC]

  1. #include "menus.h"
  2. #include "graphics.h"
  3. #include "help.h"
  4. #include "environment.h"
  5. #include "print meat.h"
  6. #include "sounds.h"
  7. #include "jotto load-save.h"
  8. #include "jotto.h"
  9. #include "jotto environment.h"
  10. #include "text twiddling.h"
  11. #include "graphics dispatch.h"
  12. #include "window layer.h"
  13. #include "program globals.h"
  14.  
  15. static    MenuHandle        gAppleMenu=0L;
  16. static    MenuHandle        gFileMenu=0L;
  17. static    MenuHandle        gEditMenu=0L;
  18. static    MenuHandle        gOptionsMenu=0L;
  19.  
  20. enum
  21. {
  22.     appleMenu = 400, fileMenu, editMenu, optionsMenu,
  23.     
  24.     aboutItem = 1, aboutMSGItem, otherMSGItem, helpPointerItem,
  25.     
  26.     newItem = 1, openItem, file_unused1, closeItem, saveItem, saveAsItem, file_unused2,
  27.         pageSetupItem, printItem, file_unused3, quitItem,
  28.     
  29.     undoItem = 1, edit_unused0, cutItem, copyItem, pasteItem, clearItem, edit_unused1, selectAllItem,
  30.     
  31.     useFiveLetter = 1, useSixLetter, options_unused1, dupToggle, nonWordsToggle,
  32.         soundToggle, animationToggle, options_unused2, showNotePad, boardFrontItem
  33. };
  34.  
  35. /*-----------------------------------------------------------------------------------*/
  36. /* internal stuff for menus.c                                                        */
  37.  
  38. static    void HandleAppleMenu(short menuItem);
  39. static    void HandleFileMenu(short menuItem);
  40. static    void HandleEditMenu(short menuItem, Boolean alreadyPassedThrough);
  41. static    void HandleOptionsMenu(short menuItem);
  42. static    void EDItem(MenuHandle theMenu, short theItem, Boolean theCondition);
  43.  
  44. Boolean InitTheMenus(void)
  45. {
  46.     Handle            MBARHandle;
  47.     
  48.     if ((MBARHandle=GetNewMBar(400))==0L)        /* sez which menus are in menu bar. */
  49.         return FALSE;
  50.     SetMenuBar(MBARHandle);                        /* set this to be THE menu bar to use. */
  51.     
  52.     if ((gAppleMenu=GetMHandle(appleMenu))==0L)    /* GetNewMBar also got menu handles of */
  53.         return FALSE;
  54.     if ((gFileMenu=GetMHandle(fileMenu))==0L)    /* every menu it includes, so just */
  55.         return FALSE;
  56.     if ((gEditMenu=GetMHandle(editMenu))==0L)    /* grab these handles and assign them */
  57.         return FALSE;
  58.     if ((gOptionsMenu=GetMHandle(optionsMenu))==0L)
  59.         return FALSE;
  60.     
  61.     AddResMenu(gAppleMenu, 'DRVR');                /* adds control panels to apple menu */
  62.     
  63.     AdjustMenus();                                /* dim/enable/check/mark menus/items */
  64.     DrawMenuBar();                                /* draws the actual menu bar */
  65.     
  66.     return TRUE;
  67. }
  68.  
  69. void ShutDownTheMenus(void)
  70. {
  71.     if (gAppleMenu!=0L)
  72.         ReleaseResource((Handle)gAppleMenu);
  73.     if (gFileMenu!=0L)
  74.         ReleaseResource((Handle)gFileMenu);
  75.     if (gEditMenu!=0L)
  76.         ReleaseResource((Handle)gEditMenu);
  77.     if (gOptionsMenu!=0L)
  78.         ReleaseResource((Handle)gOptionsMenu);
  79. }
  80.  
  81. void AdjustMenus(void)
  82. {
  83.     short            kind;
  84.     WindowPtr        front, frontDoc, mainWindow, notePad;
  85.     
  86.     front=FrontWindow();
  87.     kind=front ? ((WindowPeek)front)->windowKind : 0;
  88.     frontDoc=GetFrontDocumentWindow();
  89.     mainWindow=GetIndWindowPtr(kMainWindow);
  90.     notePad=GetIndWindowPtr(kNotePad);
  91.     
  92.     EDItem(gAppleMenu, 0, TRUE);
  93.     EDItem(gAppleMenu, aboutItem, TRUE);
  94.     EDItem(gAppleMenu, aboutMSGItem, TRUE);
  95.     EDItem(gAppleMenu, otherMSGItem, TRUE);
  96.     EDItem(gAppleMenu, helpPointerItem, TRUE);
  97.     
  98.     EDItem(gFileMenu, 0, TRUE);
  99.     EDItem(gFileMenu, newItem, TRUE);
  100.     EDItem(gFileMenu, openItem, TRUE);
  101.     EDItem(gFileMenu, closeItem, front!=0L);
  102.     EDItem(gFileMenu, saveItem, (mainWindow!=0L) && (WindowHasLayer(mainWindow)) &&
  103.         (WindowIsModifiedQQ(mainWindow)));
  104.     EDItem(gFileMenu, saveAsItem, mainWindow!=0L);
  105.     EDItem(gFileMenu, pageSetupItem, TRUE);
  106.     EDItem(gFileMenu, printItem, (frontDoc!=0L) && (WindowHasLayer(frontDoc)) &&
  107.         (GetWindowTE(frontDoc)!=0L));
  108.     EDItem(gFileMenu, quitItem, TRUE);
  109.     
  110.     EDItem(gEditMenu, 0, TRUE);
  111.     EDItem(gEditMenu, undoItem, kind<0);
  112.     EDItem(gEditMenu, cutItem, (frontDoc!=0L) && (frontDoc==notePad) && (AnyHighlightedQQ(frontDoc)));
  113.     EDItem(gEditMenu, copyItem, (frontDoc!=0L) && (frontDoc==notePad) && (AnyHighlightedQQ(frontDoc)));
  114.     EDItem(gEditMenu, pasteItem, (frontDoc!=0L) && (frontDoc==notePad) && (AnyTextInScrapQQ()));
  115.     EDItem(gEditMenu, clearItem, (frontDoc!=0L) && (frontDoc==notePad) && (AnyHighlightedQQ(frontDoc)));
  116.     EDItem(gEditMenu, selectAllItem, (frontDoc!=0L) && (frontDoc==notePad) && (AnyTextQQ(frontDoc)));
  117.     
  118.     EDItem(gOptionsMenu, 0, TRUE);
  119.     EDItem(gOptionsMenu, useFiveLetter, (FiveLetterOKQQ()) && (mainWindow==0L));
  120.     EDItem(gOptionsMenu, useSixLetter, (SixLetterOKQQ() && (mainWindow==0L)));
  121.     EDItem(gOptionsMenu, dupToggle, mainWindow==0L);
  122.     EDItem(gOptionsMenu, nonWordsToggle, mainWindow==0L);
  123.     EDItem(gOptionsMenu, soundToggle, gSoundAvailable);
  124.     EDItem(gOptionsMenu, animationToggle, TRUE);
  125.     EDItem(gOptionsMenu, showNotePad, TRUE);
  126.     EDItem(gOptionsMenu, boardFrontItem, (frontDoc!=0L) &&
  127.         (((mainWindow!=0L) && (mainWindow!=frontDoc)) ||
  128.          ((notePad!=0L) && (notePad!=frontDoc))));
  129.     
  130.     if ((frontDoc!=0L) &&
  131.         (((mainWindow==0L) || (mainWindow==frontDoc)) && (notePad!=0L)))
  132.     {
  133.         SetItem(gOptionsMenu, boardFrontItem, "\pBring note pad to front");
  134.     }
  135.     else
  136.     {
  137.         SetItem(gOptionsMenu, boardFrontItem, "\pBring board to front");
  138.     }
  139.     
  140.     CheckItem(gOptionsMenu, dupToggle, gAllowDup);
  141.     CheckItem(gOptionsMenu, nonWordsToggle, gNonWordsCount);
  142.     CheckItem(gOptionsMenu, animationToggle, gAnimation);
  143.     SetItemMark(gOptionsMenu, useFiveLetter, (gNumLetters==5) ? '◊' : noMark);
  144.     SetItemMark(gOptionsMenu, useSixLetter, (gNumLetters==6) ? '◊' : noMark);
  145.     CheckItem(gOptionsMenu, showNotePad, (notePad!=0L));
  146.     CheckItem(gOptionsMenu, soundToggle, gSoundToggle&&gSoundAvailable);
  147. }
  148.  
  149. void HandleMenu(long mSelect)
  150. {
  151.     short            menuID = HiWord(mSelect);
  152.     short            menuItem = LoWord(mSelect);
  153.     
  154.     switch (menuID)
  155.     {
  156.         case appleMenu:
  157.             HandleAppleMenu(menuItem);
  158.             break;
  159.         case fileMenu:
  160.             HandleFileMenu(menuItem);
  161.             break;    
  162.         case editMenu:
  163.             HandleEditMenu(menuItem, FALSE);
  164.             break;
  165.         case optionsMenu:
  166.             HandleOptionsMenu(menuItem);
  167.             break;
  168.     }
  169. }
  170.  
  171. void DoTheCloseThing(WindowPeek theWindow)
  172. /* a standard close procedure, called when "close" is chosen from File menu and when
  173.    a window is closed through its close box */
  174. {
  175.     short            kind;
  176.     
  177.     if (theWindow==0L)
  178.         return;
  179.     
  180.     kind = theWindow ? theWindow->windowKind : 0;
  181.     if (kind<0)        /* DA window or other system window */
  182.         CloseDeskAcc(kind);
  183.     else
  184.     {
  185.         if (WindowHasLayer((WindowPtr)theWindow))
  186.             CloseTheWindow((WindowPtr)theWindow);
  187.         else
  188.             DisposeWindow((WindowPtr)theWindow);
  189.         
  190.         AdjustMenus();
  191.     }
  192. }
  193.  
  194. void HandleAppleMenu(short menuItem)
  195. {
  196.     GrafPtr        savePort;
  197.     Str255        name;
  198.     
  199.     switch (menuItem)
  200.     {
  201.         case aboutItem:
  202.             if (!IndWindowExistsQQ(kAboutWindow))
  203.                 OpenTheIndWindow(kAboutWindow);
  204.             else
  205.                 MySelectWindow(GetIndWindowPtr(kAboutWindow));
  206.             break;
  207.         case aboutMSGItem:
  208.             if (!IndWindowExistsQQ(kAboutMSGWindow))
  209.                 OpenTheIndWindow(kAboutMSGWindow);
  210.             else
  211.                 MySelectWindow(GetIndWindowPtr(kAboutMSGWindow));
  212.             break;
  213.         case otherMSGItem:
  214.             if (!IndWindowExistsQQ(kOtherMSGWindow))
  215.                 OpenTheIndWindow(kOtherMSGWindow);
  216.             else
  217.                 MySelectWindow(GetIndWindowPtr(kOtherMSGWindow));
  218.             break;
  219.         case helpPointerItem:
  220.             if (!IndWindowExistsQQ(kHelpWindow))
  221.                 OpenTheIndWindow(kHelpWindow);
  222.             else
  223.                 MySelectWindow(GetIndWindowPtr(kHelpWindow));
  224.             break;
  225.         default:
  226.             if (menuItem > helpPointerItem+1)
  227.             {
  228.                 GetPort(&savePort);
  229.                 GetItem(gAppleMenu, menuItem, name);
  230.                 OpenDeskAcc(name);
  231.                 SetPort(savePort);
  232.             }
  233.             break;
  234.     }
  235. }
  236.  
  237. void HandleFileMenu(short menuItem)
  238. {
  239.     WindowPtr            theWindow;
  240.     TEHandle            hTE;
  241.     
  242.     switch (menuItem)
  243.     {
  244.         case newItem:
  245.             NewGame();
  246.             break;
  247.         case openItem:
  248.             LoadSaveDispatch(TRUE, FALSE, 0L);
  249.             break;
  250.         case closeItem:
  251.             if ((theWindow=GetFrontDocumentWindow())!=0L)
  252.                 DoTheCloseThing((WindowPeek)theWindow);
  253.             else
  254.                 DoTheCloseThing((WindowPeek)FrontWindow());
  255.             break;
  256.         case saveItem:
  257.             LoadSaveDispatch(FALSE, TRUE, 0L);
  258.             break;
  259.         case saveAsItem:
  260.             LoadSaveDispatch(FALSE, FALSE, 0L);
  261.             break;
  262.         case pageSetupItem:
  263.             RemoveHilitePatch();
  264.             DoThePageSetup();
  265.             InstallHilitePatch();
  266.             break;
  267.         case printItem:
  268.             theWindow=GetFrontDocumentWindow();
  269.             if (WindowHasLayer(theWindow))
  270.             {
  271.                 hTE=GetWindowTE(theWindow);
  272.                 if (hTE!=0L)
  273.                 {
  274.                     RemoveHilitePatch();
  275.                     PrintText(hTE);
  276.                     InstallHilitePatch();
  277.                 }
  278.             }
  279.             else SysBeep(7);
  280.             break;
  281.         case quitItem:
  282.             gDone=ShutDownTheProgram();
  283.             break;
  284.     }
  285. }
  286.  
  287. void HandleEditMenu(short menuItem, Boolean alreadyPassedThrough)
  288. {
  289.     short            index;
  290.     WindowPtr        frontDoc;
  291.     enum DispatchError    resultCode;
  292.     
  293.     if (!alreadyPassedThrough)
  294.         index=(gFrontWindowIsOurs) ? gFrontWindowIndex : -1;
  295.     else
  296.     {
  297.         frontDoc=GetFrontDocumentWindow();
  298.         index=(frontDoc!=0L) ? GetWindowIndex(frontDoc) : -1;
  299.     }
  300.     
  301.     if (index>=0)
  302.     {
  303.         switch (menuItem)
  304.         {
  305.             case undoItem:        resultCode=UndoDispatch(index);            break;
  306.             case cutItem:        resultCode=CutDispatch(index);            break;
  307.             case copyItem:        resultCode=CopyDispatch(index);            break;
  308.             case pasteItem:        resultCode=PasteDispatch(index);        break;
  309.             case clearItem:        resultCode=ClearDispatch(index);        break;
  310.             case selectAllItem:    resultCode=SelectAllDispatch(index);    break;
  311.             default:            resultCode=kSuccess;                    break;
  312.         }
  313.         
  314.         if ((resultCode==kPassThrough) && (!alreadyPassedThrough))
  315.             HandleEditMenu(menuItem, TRUE);
  316.     }
  317.     else SystemEdit(menuItem-1);
  318. }
  319.  
  320. void HandleOptionsMenu(short menuItem)
  321. {
  322.     WindowPtr            mainWindow, notePad;
  323.     
  324.     mainWindow=GetIndWindowPtr(kMainWindow);
  325.     notePad=GetIndWindowPtr(kNotePad);
  326.     
  327.     switch (menuItem)
  328.     {
  329.         case dupToggle:
  330.         case nonWordsToggle:
  331.         case useFiveLetter:
  332.         case useSixLetter:
  333.         case animationToggle:
  334.         case soundToggle:
  335.             switch (menuItem)
  336.             {
  337.                 case dupToggle:
  338.                     gAllowDup=!gAllowDup;
  339.                     break;
  340.                 case nonWordsToggle:
  341.                     gNonWordsCount=!gNonWordsCount;
  342.                     break;
  343.                 case useFiveLetter:
  344.                     gNumLetters=0x05;
  345.                     break;
  346.                 case useSixLetter:
  347.                     gNumLetters=0x06;
  348.                     break;
  349.                 case animationToggle:
  350.                     gAnimation=!gAnimation;
  351.                     break;
  352.                 case soundToggle:
  353.                     gSoundToggle=!gSoundToggle;
  354.                     DoSound(sound_on, TRUE);
  355.                     break;
  356.             }
  357.             break;
  358.         case showNotePad:
  359.             if (notePad!=0L)
  360.             {
  361.                 MySelectWindow(notePad);
  362.                 CloseTheWindow(notePad);
  363.             }
  364.             else
  365.             {
  366.                 OpenTheIndWindow(kNotePad);
  367.             }
  368.             break;
  369.         case boardFrontItem:
  370.             if (FrontWindow()!=0L)
  371.             {
  372.                 if ((mainWindow!=0L) && (mainWindow!=GetFrontDocumentWindow()))
  373.                 {
  374.                     MySelectWindow(mainWindow);
  375.                 }
  376.                 else if (notePad!=0L)
  377.                 {
  378.                     MySelectWindow(notePad);
  379.                 }
  380.                 else DoSound(sound_fluff, TRUE);
  381.             }
  382.             else DoSound(sound_fluff, TRUE);
  383.     }
  384. }
  385.  
  386. static    void EDItem(MenuHandle theMenu, short theItem, Boolean theCondition)
  387. {
  388.     if (theCondition)
  389.         EnableItem(theMenu, theItem);
  390.     else
  391.         DisableItem(theMenu, theItem);
  392. }
  393.